import * as common from './common.js';
import * as db from './db.js'
import {
	apiBaseUrl
} from './config.js'

// 需要登陆的，都写到这里，否则就是不需要登陆的接口
const methodsToken = [
	'userInfoInit',
	'getbeginMoneyUser',
	'UpdateUserByIdKcal',
	'UpdateUserByIdInfo',
	'userSign',
	'getSign',
	'getWeekWeight',
	'addPraise',
	'addFollow',
	'getUserFollowMenu',
	'weekManualMenu',
	'weekPlanArrange',
	'getWeekPlanArrange',
	'autoWeekMenu'
];


let loadingCount = 0;

function addLoading() {
	if (loadingCount == 0) {
		uni.showLoading({
			title: '加载中'
		});
	}
	loadingCount++
}

function isCloseLoading() {
	loadingCount--
	if (loadingCount == 0) {
		uni.hideLoading();
	}
}

const post = (url, data, Loading = 'true') => {
	if (Loading) {
		addLoading()
	}
	return new Promise((resolve, reject) => {
		let header = {
			'Accept': 'application/json',
			'Content-Type': 'application/json'
			// 'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
		};

		if (methodsToken.indexOf(url) >= 0) {
			// 判断token是否存在
			// 获取用户token
			let token = db.get("token");
			if (!token) {
				common.jumpToLogin();
				return false;
			} else {
				header.token = token;
			}
		} else {
			let token = db.get("token");
			if (token) {
				header.token = token;
			}
		}

		uni.request({
			url: apiBaseUrl + url,
			data: data,
			header: header,
			method: 'POST',
			success: (response) => {
				// uni.hideLoading();
				// isCloseLoading()
				const result = response.data;
				resolve(result);
			},
			complete: () => {
				// uni.hideLoading();
				if (Loading) {
					isCloseLoading()
				}
			},
			fail: (error) => {
				// uni.hideLoading();
				// isCloseLoading()
				if (error && error.response) {
					reject(error.response);
				}
			},
		});
	})

}

const get = (url) => {
	uni.showLoading({
		title: '加载中'
	});
	return new Promise((resolve, reject) => {
		uni.request({
			url: apiBaseUrl + url,
			header: {
				'Accept': 'application/json',
				'Content-Type': 'application/json',
				// 'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
			},
			method: 'GET',
			success: (response) => {
				uni.hideLoading();
				resolve(response.data)
			},
			fail: (error) => {
				uni.hideLoading();
				if (error && error.response) {
					reject(error.response);
				}
			},
			complete: () => {
				setTimeout(function() {
					uni.hideLoading();
				}, 250);
			}
		});
	})

}
/**
 * 所有的请求
 */
//用户登录
export const login = (data) => post('login', data);
//修改用户的初始的信息
export const upinfoLogin = (data) => post('upinfoLogin', data);
//更新用户界面显示的信息
export const userInfoInit = (data) => post('userInfoInit', data, false);
//修改用户卡里
export const UpdateUserByIdKcal = (data) => post('UpdateUserByIdKcal', data);
//修改用户信息
export const UpdateUserByIdInfo = (data) => post('UpdateUserByIdInfo', data);
//修改用户体重
export const userSign = (data) => post('userSign', data)
//获取用户信息
export const getSign = (data) => post('getSign', data, false)
//获取一周的体重记录
export const getWeekWeight = (data) => post('getWeekWeight', data, false)
//获取文章分类
export const getRticleatypeList = () => get('getRticleatypeList');
//获取指定分类的文章
export const getRticleabyType = (data) => post('getRticleabyType', data, false)
//根据文章id获取指定文章
export const getRticleaById = (data) => post('getRticleaById', data)
//根据指定时区获取菜谱信息
export const getMenuByTime = (data) => post('getMenuByTime', data)
//更具菜谱id获取菜谱信息
export const getMenuById = (data) => post('getMenuById', data)
//菜谱点赞
export const addPraise = (data) => post('addPraise', data)
//菜谱收藏
export const addFollow = (data) => post('addFollow', data)
//获取用户收藏的菜谱
export const getUserFollowMenu = (data) => post('getUserFollowMenu', data, false)
//获取全部菜谱
export const getMenuList = (data) => post('getMenuList', data)
//获取全部运动
export const getMotionList = (data) => post('getMotionList ', data)
//个人定制菜谱
export const weekManualMenu = (data) => post('weekManualMenu', data)
//根据运动定制菜谱
export const weekPlanArrange = (data) => post('weekPlanArrange', data)
//周计划输出
export const getWeekPlanArrange = (data) => post('getWeekPlanArrange', data)
//自动周计划
export const autoWeekMenu = (data) => post('autoWeekMenu', data)
export {
	apiBaseUrl
};
